<template>
  <div>
    <!-- 对话框部分 -->
    <el-dialog
      :title="isAdd ? '轮播图管理添加' : '轮播图管理编辑'"
      :visible.sync="isShow"
      :close-on-press-escape="false"
      :before-close="cancel"
    >
      <el-form :model="form" :rules="rules" ref="bannerRef">
        <el-form-item
          label="商品名称"
          :label-width="formLabelWidth"
          prop="title"
        >
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="活动图片" id="img" :label-width="formLabelWidth">
          <el-upload
            action="#"
            list-type="picture-card"
            :on-change="handleChangeFile"
            :on-exceed="onExceed"
            :file-list="fileList"
            :limit="1"
            :multiple="false"
            :auto-upload="false"
            ref="prodimg"
          >
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{ file }">
              <img
                class="el-upload-list__item-thumbnail"
                :src="imageUrl"
                alt=""
              />
              <span class="el-upload-list__item-actions">
                <span
                  class="el-upload-list__item-delete"
                  @click="handleRemove(file)"
                >
                  <i class="el-icon-delete"></i>
                </span>
              </span>
            </div>
          </el-upload>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            :active-value="1"
            :inactive-value="2"
            v-model="form.status"
            active-color="skyblue"
            inactive-color="gray"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="comfirm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import { bannerAdd, bannerEdit, bannerInfo } from "@/request/api";
import { mapActions } from "vuex";
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      imageUrl: "", //略缩图
      fileList: [], //上传图片的列表
      form: {
        title: "", //商品名称
        img: "", //图片
        status: 1,
      },
      rules: {
        title: [
          { required: true, message: "请输入轮播图标题", trigger: "blur" },
          {
            min: 2,
            max: 20,
            message: "长度在 2 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
      formLabelWidth: "120px",
    };
  },
  methods: {
    ...mapActions({
      getBannerAction: "banner/getBannerAction",
    }),
    //确认提交
    comfirm() {
      this.$refs.bannerRef.validate((val) => {
        if (val) {
          if (this.isAdd) {
            bannerAdd(this.form).then((res) => {
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getBannerAction();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            bannerEdit(this.form).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success(res.msg);
                this.cancel();
                this.getBannerAction()
                console.log("3");
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
    // 取消dialog
    cancel() {
      this.$emit("cancel", false);
      //清空
      this.reset();
    },
    //清空事件
    reset() {
      this.form = {
        title: "", //商品名称
        img: "", //图片
        status: 1,
      };
      this.fileList = [];
      //清空表单验证
      this.$refs.bannerRef.resetFields();
    },
    //点击编辑获取一条
    lookup(id) {
      console.log(id);
      bannerInfo({ id }).then((res) => {
        console.log(res);
        if (res.code == 200) {
          this.form = res.list;
          // this.fileList = [{ name: "", url: res.list.img }];
          // this.imageUrl = this.$pregImg + res.list.img;
          this.fileList = this.form.img ? [{ url: this.form.img }] : [];
          this.form.id = id;
        }
      });
    },
    //文件条数限制事件
    onExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    handleChangeFile(file) {
      this.imageUrl = this.$refs.prodimg.uploadFiles[0].url;
      this.form.img = file.raw;
    },
    //点击图片移除
    handleRemove() {
      this.fileList = [];
    },
  },
};
</script>
  
  <style scoped>
.el-upload {
  width: 100px;
  height: 100px;
}
</style>